﻿using System.Collections;

namespace U8DllPack.Controller
{
    /// <summary>
    /// listbox,combobox项类型
    /// </summary>
    public class Items : object
    {
        public object? Value { get; set; } = null;
        public string? Text { get; set; } = string.Empty;
        public Items(object? value, string? text)
        {
            Value = value;
            Text = text;
        }
        public override string? ToString()
        {
            return Text;
        }
        /// <summary>
        /// 返回listbox/combobox值对应的项对象
        /// </summary>
        /// <param name="items">控件item集合</param>
        /// <param name="val">值</param>
        /// <returns></returns>
        public static object? FindItem(IList items, object? val)
        {
            foreach (Items li in items)
            {
                if (li.Value == val)
                {
                    return li;
                }
            }
            return null;
        }
    }
    public static class GlobalClass
    {
        /// <summary>
        /// 字符串是由null还是空白字符串组成
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string? value)
        {
            if (value == null)
            {
                return true;
            }
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 移除前后空白字符（支持Null）
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string Trim_Ext(this string? str)
        {
            if (str.IsNullOrWhiteSpace())
            {
                return string.Empty;
            }
            else
            {
                return str!.Trim();
            }
        }
        /// <summary>
        /// 比较字符串是否相同(扩展)
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="newstr">待比较字符串</param>
        /// <param name="disCase">是否区分大小写：<para>默认/false：不区分，true：区分</para></param>
        /// <returns></returns>
        public static bool Equals_Ext(this string? str, string? newstr, bool disCase = false)
        {
            if (disCase)
            {
                return string.Equals(str, newstr);
            }
            else
            {
                return string.Equals(str, newstr, StringComparison.CurrentCultureIgnoreCase);
            }
        }
        /// <summary>
        /// 写数据库前转换空字符串为Null
        /// </summary>
        /// <param name="str">待转换字符串</param>
        /// <returns></returns>
        public static string? ConverEmptyToNull(this string str)
        {
            if (str.IsNullOrWhiteSpace())
            {
                return null;
            }
            return str;
        }
        /// <summary>
        /// 变量为DBNull转为Null
        /// </summary>
        /// <param name="obj">变量</param>
        /// <returns></returns>
        public static object? IsDBNullToNull(this object? obj)
        {
            if (obj == DBNull.Value) return null;
            return obj;
        }
        /// <summary>
        /// 变量为Null转为DBNull
        /// </summary>
        /// <param name="obj">变量</param>
        /// <returns></returns>
        public static object? IsNullToDBNull(this object? obj)
        {
            if (obj == null) return DBNull.Value;
            return obj;
        }
        /// <summary>
        /// 设置控件焦点-取消所有父级禁用状态
        /// </summary>
        /// <param name="ctrl"></param>
        public static void Focus_Ext(this Control? ctrl)
        {
            if (ctrl == null) return;
            Control? par_c = ctrl.Parent;
            while (par_c != null)
            {
                if (!par_c.Enabled) par_c.Enabled = true;
                par_c = par_c.Parent;
            }
            ctrl.Focus();
        }
        /// <summary>
        /// Guid是否为Null
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNull(this Guid? value)
        {
            if (value == null || value == Guid.Empty)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Guid是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmpty(this Guid value)
        {
            if (value == Guid.Empty || Convert.ToString(value).IsNullOrWhiteSpace())
            {
                return true;
            }
            return false;
        }
    }
}
